exports.GridObject = Core.MessageListener.subclass({
	classname : 'GridObject',
	
	_isSelected: false,
	_objectType: null,
	_gridLocation: null,
	_gridSize: 0,
	_obj: null,
	_markedAsRed: false,
	_objectState: 0,
	_constructionTime: 0.1,
	_width: 1,
	_height: 1,
	imgHeight: 0,
	imgWidth: 0,
    animationImageHeight: 0,
    animationImageWidth: 0,
    _placementTime : 0.0,
    _imagePath:"",
    
    _dbModel:null,
    
    _virtualItem: null,
    
    initialize: function(objectLayer){
		this._isSelected = false;
		this._objectLayer = objectLayer;
		 
		if (this._virtualItem){
			//console.log('Logger: VirtualItem was passed, Vitem = ' + this._virtualItem);
		}
		
		this.getConstructionFramesFromJSON();
	},	
	
	setObjectName: function(someName)
	{
		this._name = someName;
	},
	
	objectName: function()
	{
		return this._name;
	},
	setImagePath: function(path)
	{
		this._imagePath = path;
	},
	
	imagePath: function()
	{
		return this._imagePath;
	},
	
	setObjectState: function(someState)
	{
		this._objectState = someState;
	},
	
	objectState: function()
	{
		return this._objectState;
	},
	
	getObject: function(){
		return this._obj;
	},
	
	setWidth: function(someWidth){
		this._width = someWidth;
	},
	
	getWidth: function(){
		return this._width;
	},
	
	setPlacementTime: function(someTime){
		this._placementTime = someTime;
	},
	
	getPlacementTime: function(){
		return this._placementTime;
	},
	
	setHeight: function(someHeight){
		this._height = someHeight;
	},
	
	getHeight: function(){
		return this._height;
	},
	
	setConstructionTime: function(someTime){
		this._constructionTime = someTime;
	},
	
	getConstructionTime: function(){
		return this._constructionTime;
	},
	
	setObjectPosition: function(position){
		this._obj.setPosition(position);
	},
	
	getObjectPosition: function(){
		return this._obj.getPosition();
	},
	
	setIsSelected: function(isSelected){
		this._isSelected = isSelected;
	},
	
	getIsSelected: function(){
		return this._isSelected;
	},
	
	setOpacityToLight: function(){
		this._obj.setAlpha(0.5);
	},
	
	setOpacityToFull: function(){
		this._obj.setAlpha(1);
	},
	
	setObjectType: function(objectType){
		this._objectType = objectType;
	},
	
	getObjectType: function(){
		return this._objectType;
	},
	
	setGridLocation: function(gridLocation){
		this._gridLocation = gridLocation;
	},
	
	getGridLocation: function(){
		return this._gridLocation;
	},
	
	setGridSize: function(gridSize){
		this._gridSize = gridSize;
	},
	
	getGridSize: function(){
		return this._gridSize;
	},
	
	markAsRed: function(){
    	if (this._markedAsRed){
    		return;
    	}
    	this._obj.setColor(1, 0.25, 0.25);
    	this._markedAsRed = true;
	},
	
	unmarkAsRed: function (){
    	this._obj.setColor(1, 1, 1);
    	this._markedAsRed = false;
    },
    
    hasTimedState: function()
    {
    	return true;
    },
    
    move: function(){
    	this._objectLayer.setObjectToMovingMode(this);
    },
    
    tap: function(){
    	console.log('Logger: Parent Tap start');
    	var objectLayer = this._objectLayer;
    	if (this == objectLayer.getHighLightedObject()){
    		if (objectLayer.getIsMultiSellToolEnabled()){
    			this.sell();
    		}
    		else{
    			if (this.objectState() == ObjectState.ObjectPlacement){
    				this.makeTransaction(TransactionType.TransactionBuy);
    			}
    			objectLayer.finalizeObjectAtItsPlace();
    			if (!(objectLayer.getIsRoadToolEnabled())){
//    				GameController.getWidgetsLayer().allowMultiTools();
    			}
    		}
    	}
    	else{
    		if ((objectLayer.getIsMultiMoveToolEnabled()) && (!objectLayer.getIsMultiSellToolEnabled())){
    			this.move();
    		}
    		else if ((!objectLayer.getIsMultiMoveToolEnabled()) && (objectLayer.getIsMultiSellToolEnabled())){
    			if (objectLayer.getHighLightedObject() == null){
    				objectLayer.setHighlightedObject(this);
    				this.setOpacityToLight();
    			}
    			else{
					objectLayer.getHighLightedObject().setOpacityToFull();
    				objectLayer.setHighlightedObject(this);
    				this.setOpacityToLight();
    			}
    		}
    		else if ((!objectLayer.getIsMultiMoveToolEnabled()) && (!objectLayer.getIsMultiSellToolEnabled())){

    		}
    	}
    },
    
    getFrames: function (fileName)
    {
    	console.log('Logger: going to get Frames form file: '+fileName);
    	try
    	{
//    		GameController.getTTUtil().readFile(fileName, this.getXMLFrames.bind(this));
    		console.log('Logger: after the readFile has been completed');
    		return;
    	}
    	catch(e)
    	{
    		console.log('Logger: EXCEPTION! Reason: '+e.toString());
    	}
////    	if(GameController.getTTUtil().fileString() == '')
//    	{
//    		return;
//    	}
    },
    getFramesFromJSON: function (fileName)
    {
		var animJSONObj = null;//GameController.getAnimation(this);
    	if(animJSONObj)
    	{
    		this.animationsJSONObject = animJSONObj;
        	
    		if (this._dbModel != null)
    	   	{
        		//this.loadState(); //STEP-A
        	}
        	return;
    	}
    	console.log('Logger: going to get Frames form file: '+fileName);
    	try
    	{
    		Storage.FileSystem.readFile(fileName, false,this.getAnimationFrames.bind(this));
    		console.log('Logger: after the readFile has been completed');
    		return;
    	}
    	catch(e)
    	{
    		console.log('Logger: EXCEPTION! Reason: '+e.toString());
    	}
    },
	getConstructionAnimationFrames: function(error, text)
    {
    	console.log('Logger: in getXML Frames CallBack');
		if(!error)
		{
			//setFileString(text);
			console.log('Logger: file text is: '+text);
		}
		else
		{
			//setFileString('');
			this.constructionAnimationsJSONObject = null;
			console.log('Logger: error occurred: '+error.toString());
			return;
    	}
    	console.log('Logger: file has successfuly been read, starting parser');
    	
    	this.constructionAnimationsJSONObject = eval('('+ text + ')');
    	
    	//GameController.setConstructionAnimation(this, this.constructionAnimationsJSONObject);
    	
    	var plistPath = './Content/images/' + this._jsonObject.parent.parent.visible_id + '/' + this._jsonObject.parent.visible_id + '/' + this._jsonObject.visible_id + '/default_zip' + '/a.json';
		this.getFramesFromJSON(plistPath);
    },
    
    getAnimationFrames: function(error, text)
    {
    	console.log('Logger: in getXML Frames CallBack');
		if(!error)
		{
			//setFileString(text);
			console.log('Logger: file text is: '+text);
		}
		else
		{
			//setFileString('');
			this.animationsJSONObject = null;
			console.log('Logger: error occurred: '+error.toString());
			//if (this._dbModel != null && GameController.getObjectLayer().getHighLightedObject() == this)
			if (this._dbModel != null)
			{
				//this.loadState(); //STEP-A
	    	}
			return;
    	}
    	console.log('Logger: file has successfuly been read, starting parser');
    	
    	this.animationsJSONObject = eval('('+ text + ')');
    	
    	//GameController.setAnimation(this, this.animationsJSONObject);
    	
    	if (this._dbModel != null)
    	{
    		//this.loadState(); //STEP-A
    	}
    	
    },
    getConstructionFramesFromJSON: function()
    {
    	var animJSONObj = null;//GameController.getConstructionAnimation(this);
    	if(animJSONObj)
    	{
    		this.constructionAnimationsJSONObject = animJSONObj;
        	
        	var plistPath = './Content/images/' + this._jsonObject.parent.parent.visible_id + '/' + this._jsonObject.parent.visible_id + '/' + this._jsonObject.visible_id + '/default_zip' + '/a.json';
    		this.getFramesFromJSON(plistPath);
    		return;
    	}
    	//this.setImagePath('./Content/images/Objects/construction/'+this._width+'x'+this._height+'/a.png');
		var fileName = './Content/images/Objects/construction/'+this._width+'x'+this._height+'/a.json';
    	console.log('Logger: going to get Frames form file: '+fileName);
    	try
    	{
    		Storage.FileSystem.readFile(fileName, false,this.getConstructionAnimationFrames.bind(this));
    		console.log('Logger: after the readFile has been completed');
    		return;
    	}
    	catch(e)
    	{
    		console.log('Logger: EXCEPTION! Reason: '+e.toString());
    	}
    },
    sell: function()
    {
    	this._objectLayer.removeObject(this);
    	this.makeTransaction(TransactionType.TransactionSell);
    },
    
    makeTransaction: function(transactionType){
    	try{
	    	if (transactionType == TransactionType.TransactionBuy){
	    		if (this._virtualItem.getCost().coins > 0){
//	    			GameController.getUser().subtractCoins(parseInt(this._virtualItem.getCost().coins));
	    		}
	    		else if (this._virtualItem.getCost().bucks > 0){
//	    			GameController.getUser().subtractBucks(parseInt(this._virtualItem.getCost().bucks));
	    		}
	    		else{
	    			console.log('Logger: Buy for free');
	    		}
	    	}
	    	else if (transactionType == TransactionType.TransactionSell){
	    		console.log('Logger: Selling this item');
	    	}
    	}
    	catch(_exception){
    		console.log('Logger: Exception in makeTransaction: ' + _exception.toString());
    	}
    },
    
    destroy: function(){
        
    },
    
    updatePosition:function(){
		this._dbModel.setX(this.getGridLocation().getX());
		this._dbModel.setY(this.getGridLocation().getY());
	}
	
});